<?php

//echo var_dump($_POST[award_pilot]);
//echo var_dump($_POST[taward]);
//echo var_dump($_POST[redact_pilot]);
//echo var_dump($_POST[tredact_pilot]);
//echo var_dump($_POST[allsqpilots]);
//echo var_dump($_POST[dopilotredact]);
//echo var_dump($_POST);

	$dopost = $_POST[dopost];
	$fieldid = $_POST[fieldid];

$res_tabl_begin = "<table border=0 width=80% cellpadding=0 xellspacing=0 align=center><tr><td align=center><div class=text2>";

$res_tabl_end = "</div></td></td></table>";
	if ($loginstatus) {
        $division_r = mysql_query("select * from divisions where id='$loginstatus'");
        $division = mysql_fetch_assoc($division_r);

            if ($division[army] == 1) {
		            $FLAG = $VIEW[AFLAG];
		            $ICON = $VIEW[AICON];
            }
        	elseif ($division[army] == 2) {
		            $FLAG = $VIEW[BFLAG];
		            $ICON = $VIEW[BICON];
            }


            //field change
            if ($_POST[changefield] or $_GET[changefield] or ereg("^changefield",$navigation)) {

	            if ($dopost) {
	                $field_r = mysql_query("select * from fields_discr where id='$fieldid'");
	                $field = mysql_fetch_assoc($field_r);

                	$ftype_r = mysql_query("select * from fields_types where id='$field[type]'");
                    $ftype = mysql_fetch_assoc($ftype_r);

	            	$sqp_r = mysql_query("select id from squadrons where divisionid='$loginstatus' order by id");

 				    $i=0;

    		    	while ($res = mysql_fetch_assoc($sqp_r)) {
	               		if ($field[ammo] / ($ftype[ammo]+1) >= 0.2 and $field[fuel] / ($ftype[fuel]+1) >= 0.2) {
	                    mysql_query("insert into postponed(query,insertdate) values(\"update squadrons set fieldid='$fieldid' where id='$res[id]'\", '".date("Y.m.d H:i:s")."')");
	                    echo mysql_error();
	                 }

	              } //if dopost
               }
            }

            if ($_POST[changeinfo] or $_GET[changeinfo] or ereg("^changeinfo",$navigation)) {
	            //if ($dopost)
	            {
	                $dvname = Normalize_Text($_POST[dvname]);
					$xoname = Normalize_Reg($_POST[xoname]);
					$dvtag = Normalize_Reg($_POST[dvtag]);
	                $sqlogo = $_FILES[sqlogo][tmp_name];
	                $sqlogo_name = $_FILES[sqlogo][name];

	                $dvhomepage = Normalize_Reg($_POST[dvhomepage]);

                    $sqpass = $_POST[sqpass];
                    $sqpass2 = $_POST[sqpass2];

                    if ($sqpass and $sqpass2 and $sqpass=$sqpass2) {
	                    mysql_query("
	                    update
	                        divisions
	                    set
	                        copass='$sqpass',
	                        xopass='$sqpass'
	                    where
	                        id='$loginstatus'");
                    }

					$xoid=0;
					if ($xoname!="") {
   		                 $pilots_r = mysql_query(
  		                 "select pilots.id
  		                 	from pilots, squadrons
  		                 	where pilots.callsign LIKE '$xoname' and pilots.squadronid=squadrons.id and squadrons.divisionid='$loginstatus'");
     	      	         $xoid = mysql_result($pilots_r,0,'id');
    		    	     }


					$addsquad = $_POST[addsquad];
					if ($addsquad!="-") {
   		                 $squad_r = mysql_query("select id from squadrons where name LIKE '$addsquad'");
     	      	         if (count($squad_r)>0){
     	      	         	$sid = mysql_result($squad_r,0,'id');
     	      	         	mysql_query("
				            	update
   		   		              	squadrons
	     			            set
 			                   	divisionid=-'$loginstatus'
    			                where id='$sid'");
     	      	         	}

    		    	     }

					$delsquad = $_POST[delsquad];
					if ($delsquad!="-") {
   		                 $squad_r = mysql_query("select id from squadrons where name LIKE '$delsquad'");
     	      	         if (count($squad_r)>0){
     	      	         	$sid = mysql_result($squad_r,0,'id');
     	      	         	mysql_query("
				            	update
   		   		              	squadrons
	     			            set
 			                   	divisionid=0
    			                where id='$sid'");
     	      	         	}

    		    	     }

    		    	if ($xoid==0) {
    		    	     	$xoname=$division[coname];
    		    	     	$xoid=$division[comander];
    		    	     }


	                mysql_query("
	                update
                    	divisions
                    set
                        name='$dvname', tag='$dvtag', divisionpage='$dvhomepage', xoname='$xoname', xcomader='$xoid'
                    where
                    	id='$loginstatus'");



	                if ($sqlogo) {

	                    $sqlogo_name = explode(".",$sqlogo_name);
	                    $ext = count($sqlogo_name)-1;
	                    $sqlogo_name = $loginstatus.".".$sqlogo_name[$ext];

	                    move_uploaded_file($sqlogo,IMGPATH.$sqlogo_name);

	                    mysql_query("update divisions set squadpict='$sqlogo_name' where id='$loginstatus'");

	                } // if $sqlogo

	            } //if dopost
            }

            //squad info change
            $division_r = mysql_query("select * from divisions where id='$loginstatus'");
	        $division = mysql_fetch_assoc($division_r);

	        // division info
	        $division_r = mysql_query("select * from divisions where id='$loginstatus'");
	        $VARS[division] = mysql_fetch_assoc($division_r);

            $pilots_r = mysql_query(
               "select id, fieldid
               	from squadrons
               	where comander='$division[comander]'");
		$field_id_mas = mysql_fetch_assoc($pilots_r);

   	        $field_id = $field_id_mas['fieldid'];
   	        $co_squad = $field_id_mas['id'];

	        $VARS[basestatus] = GetBaseStatus ($field_id);
            $VARS[basestatus][planes] = GetSquadPlanes($co_squad);
            // sq supply
            $VARS[sqsupply] = GetSquadSupply ($co_squad);


			$VARS[indivision] = GetDivisionStatus($loginstatus);
			$VARS[waitdivision] = GetDivisionWaitStatus($loginstatus);

            $TPmain = new TemplateHandler();

	        ob_start();
	        include "templates/dvmanagement.html";
	        $c = ob_get_contents();
	        ob_end_clean();

	        $TPmain->AssignTagPlus("<!--CONTENT-->","&nbsp;&nbsp;&nbsp;".$err_var);
	        $TPmain->AssignTagPlus("<!--CONTENT-->",$c);

            FillTemplateCommonData();
    } // if loginstatus
    else {
		header("Location: $SiteURL");
        exit;
    }

?>